/*
 *  Copyright (c) 2011 Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU Lesser Public License v2.1
 *  which accompanies this distribution, and is available at
 *  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 *  Contributors:
 *      Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany - RMI Server and wrapper
 */
package de.ipk_gatersleben.bit.bi.edal.rmi.server.wrapper;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.List;

import javax.security.auth.Subject;

import com.healthmarketscience.rmiio.RemoteInputStream;
import com.healthmarketscience.rmiio.RemoteInputStreamClient;
import com.healthmarketscience.rmiio.RemoteOutputStream;
import com.healthmarketscience.rmiio.RemoteOutputStreamClient;

import de.ipk_gatersleben.bit.bi.edal.primary_data.DataManager;
import de.ipk_gatersleben.bit.bi.edal.primary_data.file.PrimaryDataEntityVersion;
import de.ipk_gatersleben.bit.bi.edal.primary_data.file.PrimaryDataEntityVersionException;
import de.ipk_gatersleben.bit.bi.edal.primary_data.file.PrimaryDataFile;
import de.ipk_gatersleben.bit.bi.edal.primary_data.file.PrimaryDataFileException;
import de.ipk_gatersleben.bit.bi.edal.primary_data.file.PublicReference;
import de.ipk_gatersleben.bit.bi.edal.primary_data.metadata.MetaData;
import de.ipk_gatersleben.bit.bi.edal.primary_data.metadata.MetaDataException;
import de.ipk_gatersleben.bit.bi.edal.rmi.interfaces.PrimaryDataFileRmiInterface;
import de.ipk_gatersleben.bit.bi.edal.rmi.server.EdalServer;

/**
 * Wrapper class to wrap {@link PrimaryDataFile} functions on server side.
 * 
 * @author arendd
 */
public class PrimaryDataFileWrapper extends PrimaryDataEntityWrapper implements
		PrimaryDataFileRmiInterface {

	private static final long serialVersionUID = 1L;

	/**
	 * Constructor for {@link PrimaryDataFileWrapper}.
	 * 
	 * @param dataPort
	 *            the data port for the remote objects.
	 * @param file
	 *            a {@link PrimaryDataFile} object.
	 */
	public PrimaryDataFileWrapper(int dataPort, PrimaryDataFile file)
			throws RemoteException {
		super(dataPort, file);
	}

	/** {@inheritDoc} */
	public void setMetaData(MetaData metadata, Subject subject)
			throws RemoteException, PrimaryDataEntityVersionException,
			MetaDataException {

		DataManager.setSubject(subject);

		((PrimaryDataFile) myEntity).setMetaData(metadata);
	}

	/** {@inheritDoc} */
	public void store(RemoteInputStream dataInputStream, Subject subject)
			throws RemoteException, PrimaryDataFileException,
			PrimaryDataEntityVersionException {

		DataManager.setSubject(subject);

		try {
			((PrimaryDataFile) myEntity).store(RemoteInputStreamClient
					.wrap(dataInputStream));
		} catch (IOException e) {
			EdalServer.getLogger().error(e.getMessage());
		}
	}

	/** {@inheritDoc} */
	public void read(RemoteOutputStream dataOutputStream, long versionNumber,
			Subject subject) throws RemoteException, PrimaryDataFileException {

		DataManager.setSubject(subject);

		PrimaryDataFile file = ((PrimaryDataFile) myEntity);

		for (PrimaryDataEntityVersion version : file.getVersions()) {
			if (version.getRevision() == versionNumber) {
				try {
					file.switchCurrentVersion(version);
				} catch (PrimaryDataEntityVersionException e) {
					EdalServer.getLogger().error(e.getMessage());
				}
			}
		}

		try {
			file.read(RemoteOutputStreamClient.wrap(dataOutputStream));
		} catch (IOException e) {
			EdalServer.getLogger().error(e.getMessage());
		}
	}

	/** {@inheritDoc} */
	public List<PublicReference> getPublicReferences(Subject subject)
			throws RemoteException {
		DataManager.setSubject(subject);

		return ((PrimaryDataFile) myEntity).getPublicReferences();
	}
}
